View Javadoc
1   package edu.jiangxin.apktoolbox.reverse;
2   
3   import edu.jiangxin.apktoolbox.swing.extend.listener.SelectFileListener;
4   import edu.jiangxin.apktoolbox.swing.extend.plugin.PluginPanel;
5   import edu.jiangxin.apktoolbox.utils.Constants;
6   import edu.jiangxin.apktoolbox.utils.Utils;
7   import org.apache.commons.lang3.StringUtils;
8   
9   import javax.swing.*;
10  import java.awt.*;
11  import java.awt.event.ActionEvent;
12  import java.awt.event.ActionListener;
13  
14  /**
15   * @author jiangxin
16   * @author 2019-04-12
17   *
18   */
19  public class ApkSignerPanel extends PluginPanel {
20  
21      private static final long serialVersionUID = 1L;
22  
23      private JTextField apkPathTextField;
24  
25      private JTextField keyStorePathTextField;
26  
27      private JPasswordField keyStorePasswordField;
28  
29      private JTextField aliasTextField;
30  
31      private JPasswordField aliasPasswordField;
32  
33      public ApkSignerPanel() throws HeadlessException {
34          super();
35      }
36  
37      @Override
38      public String getPluginFilename() {
39          return "apksigner.jar";
40      }
41  
42      @Override
43      public void initUI() {
44          BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
45          setLayout(boxLayout);
46  
47          createApkPathPanel();
48          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
49  
50          createKeyStorePathPanel();
51          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
52  
53          createKeyStorePasswordPanel();
54          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
55  
56          createAliasPanel();
57          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
58  
59          createAliasPasswordPanel();
60          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
61  
62          createOptionPanel();
63      }
64  
65      private void createOptionPanel() {
66          JPanel operationPanel = new JPanel();
67          operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS));
68          add(operationPanel);
69  
70          JButton recoverButton = new JButton("recover");
71          recoverButton.addActionListener(new RecoverButtonActionListener());
72  
73          JButton apkSignButton = new JButton("apksigner");
74          apkSignButton.addActionListener(new ApkSignButtonActionListener());
75  
76          operationPanel.add(recoverButton);
77          operationPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
78          operationPanel.add(apkSignButton);
79      }
80  
81      private void createAliasPasswordPanel() {
82          JPanel aliasPasswordPanel = new JPanel();
83          aliasPasswordPanel.setLayout(new BoxLayout(aliasPasswordPanel, BoxLayout.X_AXIS));
84          add(aliasPasswordPanel);
85          
86          aliasPasswordField = new JPasswordField();
87          aliasPasswordField.setText(conf.getString("apksigner.alias.password"));
88  
89          JLabel aliasPasswordLable = new JLabel("Alias Password");
90  
91          aliasPasswordPanel.add(aliasPasswordField);
92          aliasPasswordPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
93          aliasPasswordPanel.add(aliasPasswordLable);
94      }
95  
96      private void createAliasPanel() {
97          JPanel aliasPanel = new JPanel();
98          aliasPanel.setLayout(new BoxLayout(aliasPanel, BoxLayout.X_AXIS));
99          add(aliasPanel);
100         
101         aliasTextField = new JTextField();
102         aliasTextField.setText(conf.getString("apksigner.alias"));
103 
104         JLabel aliasLable = new JLabel("Alias");
105 
106         aliasPanel.add(aliasTextField);
107         aliasPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
108         aliasPanel.add(aliasLable);
109     }
110 
111     private void createKeyStorePasswordPanel() {
112         JPanel keyStorePasswordPanel = new JPanel();
113         keyStorePasswordPanel.setLayout(new BoxLayout(keyStorePasswordPanel, BoxLayout.X_AXIS));
114         add(keyStorePasswordPanel);
115         
116         keyStorePasswordField = new JPasswordField();
117         keyStorePasswordField.setText(conf.getString("apksigner.keystore.password"));
118 
119         JLabel keyStorePasswordLable = new JLabel("KeyStore Password");
120 
121         keyStorePasswordPanel.add(keyStorePasswordField);
122         keyStorePasswordPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
123         keyStorePasswordPanel.add(keyStorePasswordLable);
124     }
125 
126     private void createKeyStorePathPanel() {
127         JPanel keyStorePathPanel = new JPanel();
128         keyStorePathPanel.setLayout(new BoxLayout(keyStorePathPanel, BoxLayout.X_AXIS));
129         add(keyStorePathPanel);
130         
131         keyStorePathTextField = new JTextField();
132         keyStorePathTextField.setText(conf.getString("apksigner.keystore.path"));
133 
134         JButton keyStorePathButton = new JButton("Select KeyStore");
135         keyStorePathButton.addActionListener(new SelectFileListener("select a keystore file", keyStorePathTextField));
136 
137         keyStorePathPanel.add(keyStorePathTextField);
138         keyStorePathPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
139         keyStorePathPanel.add(keyStorePathButton);
140     }
141 
142     private void createApkPathPanel() {
143         JPanel apkPathPanel = new JPanel();
144         apkPathPanel.setLayout(new BoxLayout(apkPathPanel, BoxLayout.X_AXIS));
145         add(apkPathPanel);
146 
147         apkPathTextField = new JTextField();
148         apkPathTextField.setText(conf.getString("apksigner.apk.path"));
149 
150         JButton apkPathButton = new JButton("Select APK");
151         apkPathButton.addActionListener(new SelectFileListener("select a APK file", apkPathTextField));
152 
153         apkPathPanel.add(apkPathTextField);
154         apkPathPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
155         apkPathPanel.add(apkPathButton);
156     }
157     
158     private final class RecoverButtonActionListener implements ActionListener {
159         @Override
160         public void actionPerformed(ActionEvent e) {
161             keyStorePathTextField.setText(conf.getString("default.apksigner.keystore.path"));
162             keyStorePasswordField.setText(conf.getString("default.apksigner.keystore.password"));
163             aliasTextField.setText(conf.getString("default.apksigner.alias"));
164             aliasPasswordField.setText("default.apksigner.alias.password");
165         }
166     }
167 
168     private final class ApkSignButtonActionListener implements ActionListener {
169 
170         @Override
171         public void actionPerformed(ActionEvent e) {
172             String apkPath = checkAndGetFileContent(apkPathTextField, "apksigner.apk.path", "apk file is invalid");
173             if (StringUtils.isEmpty(apkPath)) {
174                 return;
175             }
176 
177             String keystorePath = checkAndGetFileContent(keyStorePathTextField, "apksigner.keystore.path", "keystore file is invalid");
178             if (StringUtils.isEmpty(keystorePath)) {
179                 return;
180             }
181 
182             String keystorePassword = checkAndGetStringContent(keyStorePasswordField, "apksigner.keystore.password", "keystorePassword is invalid");
183             if (StringUtils.isEmpty(keystorePassword)) {
184                 return;
185             }
186 
187             String alias = checkAndGetStringContent(aliasTextField, "apksigner.alias", "alias is invalid");
188             if (StringUtils.isEmpty(alias)) {
189                 return;
190             }
191 
192             String aliasPassword = checkAndGetStringContent(aliasPasswordField, "apksigner.alias.password", "aliasPassword is invalid");
193             if (StringUtils.isEmpty(aliasPassword)) {
194                 return;
195             }
196 
197             StringBuilder sb = new StringBuilder();
198             sb.append(getPluginStartupCmd())
199                     .append(" -keystore ").append(keystorePath).append(" -pswd ").append(keystorePassword)
200                     .append(" -alias ").append(alias).append(" -aliaspswd ").append(aliasPassword).append(" ")
201                     .append(apkPath);
202 
203             Utils.blockedExecutor(sb.toString());
204         }
205     }
206 }